package com.nuoniu.sibanyun.service.erp.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.dto.ErpReportProgrammeSearchDto;
import com.nuoniu.sibanyun.entity.erp.ErpReportDesign;
import com.nuoniu.sibanyun.entity.erp.ErpReportProgramme;
import com.nuoniu.sibanyun.mapper.erp.ErpReportProgrammeMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpReportDesignService;
import com.nuoniu.sibanyun.service.erp.IErpReportProgrammeService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-02-02
 */
@Service
public class ErpReportProgrammeServiceImpl extends ServiceImpl<ErpReportProgrammeMapper, ErpReportProgramme> implements IErpReportProgrammeService {


    @Autowired
    private IErpAccountService erpAccountService;
    @Autowired
    private IErpReportDesignService erpReportDesignService;

    @Override
    public List<ErpReportProgramme> listPersonalErpReportProgrammes(ErpReportProgrammeSearchDto erpReportProgrammeSearchDto) {
        String reportCode = erpReportProgrammeSearchDto.getReportCode();
        Integer businessType = erpReportProgrammeSearchDto.getBusinessType();
        if (StringUtils.isBlank(reportCode))throw new NuoNiuException("参数错误");
        UserInfo userInfo = UserThreadLocal.get();
        Integer currentAccountId = erpAccountService.getCurrentAccountId();
        QueryWrapper<ErpReportProgramme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("account_id",currentAccountId);
        queryWrapper.eq("create_user_id",userInfo.getUserId());
        queryWrapper.eq("report_code",reportCode);
        if (null != businessType){
            queryWrapper.eq("business_type",businessType);
        }
        List<ErpReportProgramme> list = list(queryWrapper);
        return list;
    }

    @Override
    public List<ErpReportDesign> getSystemReportProgramme(String reportCode) {
        List<ErpReportDesign> list = erpReportDesignService.list(reportCode);
        return list;
    }

    @Override
    public void savePersonalErpReportProgramme(ErpReportProgramme erpReportProgramme) {
        String reportCode = erpReportProgramme.getReportCode();
        String programmeJson = erpReportProgramme.getProgrammeJson();
        if (StringUtils.isBlank(reportCode) || StringUtils.isBlank(programmeJson) || StringUtils.isBlank(erpReportProgramme.getProgrammeName()))throw new NuoNiuException("参数错误");
        //校验
        List<ErpReportDesign> erpReportDesigns = JSONObject.parseArray(programmeJson, ErpReportDesign.class);
        if (reportCode.contains("_Detail_")){
            erpReportDesignService.checkDesign(erpReportDesigns,"_Detail_");
        }else if(reportCode.contains("_Statistic_")){
            //校验 字段设计
            erpReportDesignService.checkDesign(erpReportDesigns,"_Statistic_");
        }
        UserInfo userInfo = UserThreadLocal.get();
        Integer currentAccountId = erpAccountService.getCurrentAccountId();
        Long id = erpReportProgramme.getId();
        LocalDateTime now = LocalDateTime.now();
        if (null == id){
            erpReportProgramme.setAccountId(currentAccountId);
            erpReportProgramme.setCompanyId(userInfo.getCompanyId());
            erpReportProgramme.setCreateUserId(userInfo.getUserId());
            erpReportProgramme.setCreateUserName(userInfo.getRealName());
            erpReportProgramme.setCreateDate(now);
        }else{
            ErpReportProgramme oldErpReportProgramme = getById(id);
            if (!oldErpReportProgramme.getAccountId().equals(currentAccountId))throw new NuoNiuException("不能修改其他账套下的数据");
            if (!oldErpReportProgramme.getCreateUserId().equals(userInfo.getUserId()))throw new NuoNiuException("不能修改其他人的方案数据");
            erpReportProgramme.setUpdateUserId(userInfo.getUserId());
            erpReportProgramme.setUpdateUserName(userInfo.getRealName());
            erpReportProgramme.setUpdateDate(now);
        }
        saveOrUpdate(erpReportProgramme);
    }

    @Override
    public void deletePersonalErpReportProgramme(Long id) {
        removeById(id);
    }
}
